package org.spiderflow.controller;

import cn.dev33.satoken.stp.StpUtil;
import org.spiderflow.core.dto.UserDto;
import org.spiderflow.core.model.User;
import org.spiderflow.core.service.UserService;
import org.spiderflow.model.JsonBean;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author fanyushuai
 * @version 1.0
 * @description: TODO
 * @date 2021/12/6 18:08
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @RequestMapping("/login")
    public JsonBean login(@RequestBody(required = true) UserDto userDto){
        Map<String,String> data = new HashMap<>();
        if(userService.login(userDto)){
            data.put("token",StpUtil.getTokenValue());
        }
        return new JsonBean<Map<String,String>>(data,200);
    }

    @RequestMapping("/logout")
    public JsonBean logout(){
        StpUtil.logout();
        return new JsonBean<Boolean>(true);
    }
}
